// <copyright file="MainForm.cs" company="DotNetSide">
// Copyright (c) 2008 All Right Reserved
// </copyright>
// <author>fabio.cozzolino</author>
// <date>30/06/2008 18.09.35</date>

namespace DotNetSide.SqlScriptGenerator
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    using System.Data.SqlClient;
    using System.Reflection;

    public partial class MainForm : Form
    {
        private PropertyWindow propertyWindow;
        private ServerExplorerWindow serverWindow;
        private List<DatabaseWindow> databaseWindows;

        public MainForm()
        {
            InitializeComponent();

            this.serverWindow = new ServerExplorerWindow();
            this.serverWindow.RightToLeftLayout = RightToLeftLayout;
            this.serverWindow.OnDataBaseChanged += new ServerExplorerWindow.DataBaseChanged(serverExplorer_OnDataBaseChanged);

            this.propertyWindow = new PropertyWindow();

            this.databaseWindows = new List<DatabaseWindow>();

            AssemblyName currentAssembly = Assembly.GetExecutingAssembly().GetName();
            this.Text = Application.ProductName + " " + currentAssembly.Version.ToString(4);
        }

        void serverExplorer_OnDataBaseChanged(object sender, string serverName, string databaseName)
        {
            string tabText = serverName + "." + databaseName;
            DatabaseWindow databaseExplorer = this.databaseWindows.Find(delegate(DatabaseWindow current){
                return current.TabText == tabText;});
            if (databaseExplorer == null)
            {
                ServerExplorerWindow se = sender as ServerExplorerWindow;

                databaseExplorer = new DatabaseWindow();
                databaseExplorer.TabText = tabText;
                databaseExplorer.LoadTables(se.GetConnectionString(serverName, databaseName), databaseName);
                databaseExplorer.FormClosed += new FormClosedEventHandler(databaseExplorer_FormClosed);
                this.databaseWindows.Add(databaseExplorer);
            }
            databaseExplorer.Show(this.MainDockPanel);
            this.propertyWindow.propertyGrid.SelectedObject = databaseExplorer.Options;
            this.propertyWindow.Show(this.MainDockPanel);
        }

        void databaseExplorer_FormClosed(object sender, FormClosedEventArgs e)
        {
            databaseWindows.Remove((DatabaseWindow)sender);
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            ConnectionForm connectForm = new ConnectionForm();
            if (connectForm.ShowDialog(this) == DialogResult.OK)
            {
                this.serverWindow.Servers = new string[] { connectForm.Server };
                this.serverWindow.AauthenticationMethod = connectForm.AuthenticationMethod;
                this.serverWindow.Username = connectForm.Username;
                this.serverWindow.Password = connectForm.Password;
                this.serverWindow.Show(this.MainDockPanel);

            }
            else
            {
                this.Close();
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SettingsWindow settings = new SettingsWindow();
            settings.ShowDialog();
        }
    }
}